package gamagora.game.entity
{
	import gamagora.game.entity.OSEntity;
	import gamagora.game.resources.OSImages;
	import gamagora.game.resources.OSSounds;
	import gamagora.game.world.maps.OSMap;
	
	import org.flixel.FlxG;
	import org.flixel.FlxSprite;
	import org.flixel.system.FlxAnim;
	
	public class OSMonkey extends OSEntity
	{
		public static const IDLE:String = "idle";
		public static const START_RUN:String = "start run";
		public static const STOP_RUN:String = "stop run";
		public static const RUN:String = "run";
		public static const PLAYER_DIST_BEFORE_MOVE:Number = 50.0;
		
		private var _move:Boolean;
		private var _moveToX:Number;
		private var _speed:Number;
		private var _whenPlayerCome:Boolean;
		private var _background:Boolean;
		private var _animOnly:Boolean;
		private var _canCry:Boolean;
		
		public function OSMonkey(X:Number=0, Y:Number=0, background:Boolean = false, startMove:Number = 0, runFramerateNoBack:Number = 40, redRibbon:Boolean = false)
		{
			super(ANIMATION, X, Y);
			_background = background;
			_canCry = true;
			
			if(_background)
			{
				loadGraphic(OSImages.MONKEY2, true, true, 65, 65);
				offset.x = 32;
				offset.y = 27;
				
				_speed = 20.0;
				
				addAnimation(IDLE, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 20);
				addAnimation(START_RUN, [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38], 60, false);
				addAnimation(STOP_RUN, [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58], 20, false);
				addAnimation(RUN, [105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125], 20);
			}
			else 
			{
				loadGraphic(redRibbon ? OSImages.MONKEY3 : OSImages.MONKEY1, true, true, 128, 128);
				offset.x = 64;
				offset.y = 34;
				
				_speed = 40.0;
				
				addAnimation(IDLE, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 20);
				addAnimation(START_RUN, [20, 21, 22, 23, 34, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38], 120, false);
				addAnimation(STOP_RUN, [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58], 40, false);
				addAnimation(RUN, [85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109], runFramerateNoBack);
			}
			
			play(IDLE);
			
			_moveToX = 0;
			_animOnly = false;
			
			if(startMove != 0)
			{
				moveFrom(startMove);
			}
		}
		
		public override function update():void
		{
			super.update();
			
			if(!_background && _canCry)
			{
				generateMonkeyCry(0.35, 0.07);
			}
			
			if(finished)
			{
				switch(_curAnim.name)
				{
					case START_RUN:
						play(RUN);
						break;
					case STOP_RUN:
						play(IDLE);
						break;
				}
			}
			
			if(!_animOnly)
			{
				if(_whenPlayerCome && Math.abs(x + (_background ? _scroll : 0) - _world.getPlayerX()) < PLAYER_DIST_BEFORE_MOVE)
				{
					play(START_RUN, true);
					_whenPlayerCome = false;
				}
				
				if(_move && _curAnim.name == RUN)
				{
					if(x < _moveToX)
					{
						x += FlxG.elapsed * _speed;	
						if(x >= _moveToX)
						{
							x = _moveToX;
							play(STOP_RUN);
							_move = false;
						}
					}
					else 
					{
						x -= FlxG.elapsed * _speed;
						if(x <= _moveToX)
						{
							x = _moveToX;
							play(STOP_RUN);
							_move = false;
						}
					}
				}
			}
		}
		
		public override function setStartX(decalX:Number):void
		{
			super.setStartX(decalX);
			_moveToX += decalX;
		}
		
		public function moveFrom(newX:Number, whenPlayerCome:Boolean = true):void
		{
			_move = true;
			_whenPlayerCome = whenPlayerCome;
			_moveToX = x + newX;
			_facing = x < _moveToX ? LEFT : RIGHT;
			if(!_whenPlayerCome)
			{
				play(START_RUN, true);
			}
		}
		
		private function generateMonkeyCry(rate:Number = 0.5, volume:Number = 0.2):void
		{
			rate = (1 - rate) / 1000 + 0.999;
			var rand:Number = Math.random();
			if(rand > rate)
			{
				rand = Math.random();
				if(rand < 0.33)
				{
					OSSounds.playMonkeyCry1(volume);
				}
				else if(rand > 0.66)
				{
					OSSounds.playMonkeyCry2(volume);
				}
				else
				{
					OSSounds.playMonkeyCry3(volume);
				}
			}
		}

		public function get move():Boolean
		{
			return _move;
		}

		public function get animOnly():Boolean
		{
			return _animOnly;
		}

		public function set animOnly(value:Boolean):void
		{
			_animOnly = value;
		}

		public function get curAnim():FlxAnim
		{
			return _curAnim;
		}

		public function get canCry():Boolean
		{
			return _canCry;
		}

		public function set canCry(value:Boolean):void
		{
			_canCry = value;
		}

	}
}
